package com.wslds.blog.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class SessionInterceptor implements Interceptor {

	private static final long serialVersionUID = -4644971020599658493L;

	public void destroy() {
		// empty
	}

	public void init() {
		// empty
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		HttpSession sessionMap = ServletActionContext.getRequest().getSession();
		String staffId = (String) sessionMap.getAttribute("meId");
		HttpServletRequest request = ServletActionContext.getRequest();
//		HttpServletResponse response = ServletActionContext.getResponse();
		String path = request.getRequestURI();
		
		if( path.startsWith("/wsldsBlog/bk/") || 
				path.startsWith("/bk/")){
			if (staffId != null ) {
				String result = invocation.invoke();
				return result;
			}
			ActionContext.getContext().put("msg", "XXXXXXXXX");
			return "relogin";
		}else{
			return invocation.invoke();
		}
		
		
	}

	@SuppressWarnings("unused")
	private boolean isLoginOrLoginOut(String actionName) {
		return "login".equalsIgnoreCase(actionName)
				|| "logout".equalsIgnoreCase(actionName);
	}

}
